//
//  TrainIndexViewController.m
//  ELife
//
//  Created by  刘奇男 on 11-6-8.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "TrainIndexViewController.h"
#import "SearchByStationResultViewController.h"
#import "SearchByLineResultViewController.h"
#import "AlertView.h"

@implementation TrainIndexViewController

@synthesize choseEndCityButton, choseStartCityButton, choseType, startCityTextField, endCityTextField, choseCityBack, segmentControlBack, searchStationButton, searchLineBack
    ,lineTextField ,datePickerView, choseDate, date, dateLabel;

static int type;

/**
 12.1 火车票站站查询接口
 请求地址: /trainline.php
 请求参数:   
 action    = 'get_trainline_by_station';
 
 begin_station = 起始城市 
 end_station = 抵达城市
 date = 日期 例子:03-15
 
 返回参数: 
 
 train_code  	车次
 start_station   发站
 end_station     到站
 start_time 	发时
 end_time 	到时
 times 		历时
 b_station 始发站
 e_station 终点站
 type 列车类型
 返回格式事例: 
 <?xml version="1.0" encoding="UTF-8"?><msg><head><returncode>0</returncode><transcode>get_trainline_by_station</transcode></head><body><trainlines><trainline><train_code>1302</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>05:40</start_time><end_time>21:27</end_time><times>15:47</times><b_station>满洲里</b_station><e_station>北京</e_station><type>普快</type></trainline><trainline><train_code>D24</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>07:13</start_time><end_time>13:30</end_time><times>06:17</times><b_station>长春</b_station><e_station>北京</e_station><type>动车</type></trainline><trainline><train_code>D74</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>10:42</start_time><end_time>17:05</end_time><times>06:23</times><b_station>吉林</b_station><e_station>北京</e_station><type>动车</type></trainline><trainline><train_code>D28</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>10:50</start_time><end_time>17:14</end_time><times>06:24</times><b_station>哈尔滨</b_station><e_station>北京</e_station><type>动车</type></trainline><trainline><train_code>D102</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>11:09</start_time><end_time>17:58</end_time><times>06:49</times><b_station>哈尔滨</b_station><e_station>北京</e_station><type>动车</type></trainline><trainline><train_code>T158</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>12:09</start_time><end_time>20:09</end_time><times>08:00</times><b_station>哈尔滨</b_station><e_station>泰州</e_station><type>特快</type></trainline><trainline><train_code>1174</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>12:55</start_time><end_time>02:19</end_time><times>13:24</times><b_station>哈尔滨</b_station><e_station>太原</e_station><type>普快</type></trainline><trainline><train_code>D22</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>15:18</start_time><end_time>21:38</end_time><times>06:20</times><b_station>长春</b_station><e_station>北京</e_station><type>动车</type></trainline><trainline><train_code>D26</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>17:26</start_time><end_time>23:33</end_time><times>06:07</times><b_station>哈尔滨</b_station><e_station>北京</e_station><type>动车</type></trainline><trainline><train_code>K340</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>21:52</start_time><end_time>10:16</end_time><times>12:24</times><b_station>佳木斯</b_station><e_station>北京</e_station><type>快速</type></trainline><trainline><train_code>Z62</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>22:10</start_time><end_time>06:00</end_time><times>07:50</times><b_station>长春</b_station><e_station>北京</e_station><type>直特</type></trainline><trainline><train_code>T60</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>22:34</start_time><end_time>07:57</end_time><times>09:23</times><b_station>长春</b_station><e_station>北京</e_station><type>特快</type></trainline><trainline><train_code>K266</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>22:40</start_time><end_time>10:37</end_time><times>11:57</times><b_station>牡丹江</b_station><e_station>北京</e_station><type>快速</type></trainline><trainline><train_code>T272</train_code><start_station>长春</start_station><end_station>北京</end_station><start_time>23:55</start_time><end_time>09:40</end_time><times>09:45</times><b_station>吉林西</b_station><e_station>北京</e_station><type>特快</type></trainline></trainlines></body></msg>
 
 
 
 
 
 
 
 12.2 火车票车次查询接口
 请求地址: /trainline.php
 请求参数:   
 action    = 'get_trainline_by_train';
 train_code  车次
 date = 日期 例子:03-15
 
 返回参数: 
 train_code 车次
 station 停靠站
 start_time 发时
 end_time 到时
 times 历时
 
 
 
 返回格式事例: 
 <?xml version="1.0" encoding="UTF-8"?><msg><head><returncode>0</returncode><transcode>get_trainline_by_train</transcode></head><body><trainline><trainstation><train_code>T272</train_code><station>吉林西</station><start_time>21:36</start_time><end_time>----</end_time><times>--</times></trainstation><trainstation><train_code>T272</train_code><station>九台</station><start_time>22:42</start_time><end_time>22:38</end_time><times>01:02</times></trainstation><trainstation><train_code>T272</train_code><station>长春</station><start_time>23:55</start_time><end_time>23:20</end_time><times>01:44</times></trainstation><trainstation><train_code>T272</train_code><station>铁岭</station><start_time>01:55</start_time><end_time>01:53</end_time><times>04:17</times></trainstation><trainstation><train_code>T272</train_code><station>沈阳北</station><start_time>02:42</start_time><end_time>02:34</end_time><times>04:58</times></trainstation><trainstation><train_code>T272</train_code><station>锦州南</station><start_time>04:57</start_time><end_time>04:44</end_time><times>07:08</times></trainstation><trainstation><train_code>T272</train_code><station>山海关</station><start_time>06:47</start_time><end_time>06:37</end_time><times>09:01</times></trainstation><trainstation><train_code>T272</train_code><station>北京</station><start_time>----</start_time><end_time>09:40</end_time><times>12:04</times></trainstation></trainline></body></msg>
 */

- (NSString*)formatDate:(NSDate *)formatDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日"];
    
    NSString *stringFromDate = [formatter stringFromDate: formatDate];
    return stringFromDate;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [choseEndCityButton release];
    [choseStartCityButton release];
    [choseType release];
    [startCityTextField release]; 
    [endCityTextField release];
    [choseCityBack release];
    [segmentControlBack release];
    [searchStationButton release];
    [searchLineBack release];
    [lineTextField release];
    [datePickerView release]; 
    [choseDate release];
    [date release];
    [dateLabel release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

- (CGRect)pickerFrameWithSize:(CGSize)size
{
	CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
	CGRect pickerRect = CGRectMake(	0.0,
                                   screenRect.size.height - 42.0 - size.height,
                                   size.width,
                                   size.height);
	return pickerRect;
}

- (void)showDatePicker
{
    datePickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    datePickerView.datePickerMode = UIDatePickerModeDate;
	
//    NSLog(@"------SELF DATE: %@\n", self.date);
//    datePickerView.date = self.date;

	[self.view addSubview:choseDate];
}

-(void) showStationSearch{
    type = 0;
    [searchLineBack removeFromSuperview];
    
    [choseCityBack setFrame:CGRectMake(20, 100, 280, 264)];
    [self.view addSubview:choseCityBack];
}

-(void) showLineSearch{
    type = 1;
    [choseCityBack removeFromSuperview];
    
    [searchLineBack setFrame:CGRectMake(20, 100, 280, 110)];
    [self.view addSubview:searchLineBack];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [startCityTextField setDelegate:self];
    [endCityTextField setDelegate:self];
    [lineTextField setDelegate:self];
        
    choseCityBack.layer.cornerRadius = 8;
    choseCityBack.layer.masksToBounds = YES;
    
    segmentControlBack.layer.cornerRadius = 8;
    segmentControlBack.layer.masksToBounds = YES;
    
    searchLineBack.layer.cornerRadius = 8;
    searchLineBack.layer.masksToBounds = YES;
    
    self.date = [[NSDate date] retain];
    [dateLabel setText:[self formatDate:date]];
    
    [self showStationSearch];
    
    [self.navigationItem setTitle:@"火车票查询"];
    
    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
	temporaryBarButtonItem.title = @"返回";
	self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
	[temporaryBarButtonItem release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)choseType:(id)sender
{
    UISegmentedControl *segmentControl = (UISegmentedControl *)sender;
    unsigned int selectedIndex = [segmentControl selectedSegmentIndex];
    
    switch (selectedIndex) {
        case 0:
            [self showStationSearch];
            break;
        case 1:
            [self showLineSearch];
            break;
    }
}

-(IBAction) choseDate:(id)sender
{
    [self showDatePicker];
}

-(IBAction) searchStation:(id)sender
{
    if ([startCityTextField.text length] == 0
        || [endCityTextField.text length] == 0
        ){
        [AlertView showAlert:@"请填写完整"];
        return;
    }
    
    SearchByStationResultViewController *controller = [[SearchByStationResultViewController alloc] initWithNibName:@"SearchByStationResultView" bundle:nil];
    [controller setQueryParam:[self.startCityTextField text] endCity:[self.endCityTextField text] date:self.date];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];

}

-(IBAction) searchLine:(id)sender
{     
    if ([lineTextField.text length] == 0
          ){
        [AlertView showAlert:@"请填写完整"];
        return;
    }
    SearchByLineResultViewController *controller = [[SearchByLineResultViewController alloc] initWithNibName:@"SearchByLineResultView" bundle:nil];
    [controller setQueryParam:[self.lineTextField text]];
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}

-(IBAction) confirmDate:(id) sender
{
    NSLog(@"---------ChoseDate:------%@\n",[datePickerView date]);
    self.date = [datePickerView date];
    [choseDate removeFromSuperview];
    
    
    [dateLabel setText:[self formatDate:self.date]];
}

-(IBAction) cancelDatePicker:(id) sender
{
    [choseDate setHidden:YES];
}

#pragma mark text回调
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
	return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
	[textField resignFirstResponder];
	return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
	[textField resignFirstResponder];
	return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
	[textView resignFirstResponder];
	return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return YES;
}

@end
